library(arm)
library(rstan)
library(rstanarm)
library(foreign)
library(plyr)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

megapoll.reshaped <- read.dta("gay_marriage_megapoll_updated.dta")
N05 <- normal(0, 5)

gay.mar.stan.model <-  stan_glmer(favor_marriage_comb ~ 
                                    year_trend_rescaled + year_trend_squared_rescaled
                                  + (1 + year_trend_rescaled + year_trend_squared_rescaled | state)
                                  + (1 | age) 
                                  + (1 | edu) 
                                  + (1 | female_race_wbh)
                                  +  dempresvote + p_relig
                                  , prior = N05, prior_intercept = N05
                                  ,  data=megapoll.reshaped, family=binomial,
                                   iter = 500, chains = 4, thin=1)

save(gay.mar.stan.model, file="gay_mar_STAN_quadratic_trend_by_state.Rdata")




